package jsyf.oa.web.forms;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.forms.FormsFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by 92852 on 2018/6/22.
 */
@Api(value = "自定义表单", description = "自定义表单")
@Controller
@RequestMapping("/forms")
public class FormsController {
    @Resource
    private FormsFacade formsFacade;

    @ResponseBody
    @RequestMapping(value = "/getComponents.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取组件信息",
            notes = "获取组件信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{}", dataType = "String", paramType = "body")
    public void getComponents(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = formsFacade.getComponents(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrYearTimeRespond(records));
    }

    /*保存表单字段信息*/
    @ResponseBody
    @RequestMapping(value = "/saveInfo.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "保存",
            notes = "保存",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{'forms':[ {description:'',compId:'',prompt:''}]}", dataType = "String", paramType = "body")
    public void saveInfo(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = formsFacade.saveInfo(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrHourTimeRespond(records));
    }

    /*保存表单创建信息*/
    @ResponseBody
    @RequestMapping(value = "/saveCreaterInfo.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "保存表单创建信息",
            notes = "保存表单创建信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{createUserId:'',formsName:''}", dataType = "String", paramType = "body")
    public void saveCreaterInfo(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = formsFacade.saveCreaterInfo(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrHourTimeRespond(records));
    }

    /*获取个人表单信息*/
    @ResponseBody
    @RequestMapping(value = "/getFormsCreate.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取个人表单信息",
            notes = "获取个人表单信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:''}", dataType = "String", paramType = "body")
    public void getFormsCreate(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = formsFacade.getFormsCreate(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrYearTimeRespond(records));
    }
    /*获取表单详情*/
    @ResponseBody
    @RequestMapping(value = "/getFieldsAndComponents.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取个人表单信息",
            notes = "获取个人表单信息",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:''}", dataType = "String", paramType = "body")
    public void getFieldsAndComponents(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = formsFacade.getFieldsAndComponents(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrYearTimeRespond(records));
    }

}
